<template>
  <div class="order-wrapper">
  	<mt-header fixed :title="orderobj.id+'待接订单'">
  		<router-link to="/home/list" slot="left">
		    <mt-button icon="back">返回</mt-button>
		</router-link>
  	</mt-header>
  	<div class="order-wrapper-context">
  		<m-orderdetail :orderobj="orderobj" :isEmergency="orderobj.isEmergency"></m-orderdetail>
		<div class="btn-cancel">
			<mt-button type="primary" @click="getorder" :disabled="acOrderId">接取订单</mt-button>
		</div>
  	</div>
  </div>
</template>

<script>
import mOrderdetail from '@/components/basecom/orderdetail';
import {mapGetters, mapMutations} from 'vuex';
import axios from '@/api/axios';
export default {
	data: function () {
		return {
			orderobj: {
				id: this.$route.query.id,
				kilometre: this.$route.query.kilometre,
				truckload: this.$route.query.truckload,
				isEmergency: parseInt(this.$route.query.isEmergency),
				datalist: null
			}
		}
	},
	created: function () {
		this.orderobj.datalist = this.getAcStoreMsg();
	},
	methods: {
		getorder: function () {
			var acOrderId = this.acOrderId;
			if(acOrderId) {
				this.$toast('请完成当前订单');
			}else {
				this.sendOrder();
			}
		},
		sendOrder: function () {
			var orderobj = this.orderobj;
			var id = orderobj.id;
			var phone = this.getId();
			var setting = {
				method: 'POST',
				url: '/getTruckAcceptOrders',
				headers: {
					'Content-Type': 'application/json'
				}
			}
			var data = {
				phone: phone,
				id: id
			};
			axios((res) => {
				if(res.code === 0) {
					var orderid = id;
					var order = {
						id: id,
						orderid: orderid,
						orderstate: true,
						ordertitle: orderid,
						kilometre: orderobj.kilometre,
						truckload: orderobj.truckload
					};
					this.setAcOrderMsg(orderobj.datalist);
					this.setAcorder(order);
					this.$toast('接单成功');
					this.$router.push({path: '/home/list'});
				}else {
					if(res.code === 401) return this.$toast(res.data);
					this.$toast('接单失败');
				}
			},{setting, data});
		},
		...mapMutations(['setAcorder','setAcOrderMsg']),
		...mapGetters(['getAcStoreMsg', 'getOrderId', 'getId'])
	},
	components: {
		mOrderdetail
	},
	computed: {
		acOrderId: {
			get: function () {
				return this.getOrderId() !== null;
			}
		}
	}
}
</script>

<style>
</style>
